<template>
    <div>
        <AppMain></AppMain>
        <button @click="changeStudent">改数值</button>
    </div>
</template>

<script>
// 导入computed函数
import { computed } from 'vue';
import AppMain from './components/AppMain.vue';

export default {
    data() {
        return {
            title: '这是一个依赖注入案例',
            student: {
                name: '小明',
                age: 22,
                gender: '男'
            }
        }
    },
    components: {
        AppMain,
    },
    // 要用provide方法，不然this就不对了，获取不到data信息了
    provide() {
        return {
            title: computed(() => {
                return this.title;
            }),
            student: this.student
        }
    },
    methods: {
        changeStudent() {
            this.student.name = '长孙无敌';
            this.student.age = 33;
            this.title = '网站已经被黑';
        }
    },
}
</script>

<style scoped></style>